/*
Copyright (c) 2019 Alibaba Group Holding Limited

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/
//*******************************
//             ASCON_AEAD
//*******************************
#include "ascon_aead_de.h"

/**
  * @brief  初始化进行加密算法的关键参数
  * @param  ascon_aead_initstruct : 初始化结构体
  * @retval None
  */

void Ascon_Aead_De_Init(Ascon_Aead_De_InitTypeDef* ascon_aead_initstruct)
{
    ASCON_AEAD_DE_REG->MBN = ascon_aead_initstruct->msg_block_num; 
    ASCON_AEAD_DE_REG->MA  = ascon_aead_initstruct->msg_addr; 
    ASCON_AEAD_DE_REG->ACM = ascon_aead_initstruct->aead_ctrl_mode;
    ASCON_AEAD_DE_REG->AHA = ascon_aead_initstruct->aead_c_addr;
    ASCON_AEAD_DE_REG->AAL = ascon_aead_initstruct->aead_a[0];//附加信息
    ASCON_AEAD_DE_REG->AAH = ascon_aead_initstruct->aead_a[1];//附加信息
    ASCON_AEAD_DE_REG->AK0 = ascon_aead_initstruct->aead_k[0];//秘钥 
    ASCON_AEAD_DE_REG->AK1 = ascon_aead_initstruct->aead_k[1];//秘钥
    ASCON_AEAD_DE_REG->AK2 = ascon_aead_initstruct->aead_k[2];//秘钥
    ASCON_AEAD_DE_REG->AK3 = ascon_aead_initstruct->aead_k[3];//秘钥 
    ASCON_AEAD_DE_REG->AN0 = ascon_aead_initstruct->aead_n[0];//随机数  
    ASCON_AEAD_DE_REG->AN1 = ascon_aead_initstruct->aead_n[1];//随机数 
    ASCON_AEAD_DE_REG->AN2 = ascon_aead_initstruct->aead_n[2];//随机数 
    ASCON_AEAD_DE_REG->AN3 = ascon_aead_initstruct->aead_n[3];//随机数     
    Ascon_Aead_De_Send_Msg(ascon_aead_initstruct);
}

/**
  * @brief  向进行加密算法中写入待加密的数据
  * @param  msg_data : 待进行加密的数据
  * @retval none
  */

void Ascon_Aead_De_Send_Msg(Ascon_Aead_De_InitTypeDef* ascon_aead_initstruct)
{ 
    u32 index;
    u32 num;
    u32 msg_addr;
    u32 mode;
    mode = ascon_aead_initstruct->aead_ctrl_mode;
    if (mode == 0x00000001)
      if (ascon_aead_initstruct -> msg_block_num <= 128)
        num = ascon_aead_initstruct -> msg_block_num * 2;
      else
        num = 128 * 2;
    else 
      if (ascon_aead_initstruct -> msg_block_num <= 64)
        num = ascon_aead_initstruct -> msg_block_num * 4;
      else
        num = 64 * 4;
    msg_addr = ascon_aead_initstruct -> msg_addr;
    for (index = 0; index < num; index++)
    {
        REG32(ASCON_AEAD_DE_RX_BASE + msg_addr + index * 4) = *ascon_aead_initstruct->msg_data;
        ascon_aead_initstruct->msg_data++;
    }
}

/**
  * @brief  开始算法
  * @param  ascon_aead_initstruct
  * @retval none
  */

void Start_Ascon_Aead_De(Ascon_Aead_De_InitTypeDef* ascon_aead_initstruct)
{   
    ASCON_AEAD_DE_REG->ACE = ascon_aead_initstruct->aead_ctrl_en;
}

/**
  * @brief  是否完成加密算法
  * @param  void
  * @retval none
  */

u8 Whether_Ascon_De_Aead(void)
{   
    if (REG32(ASCON_AEAD_DE_REG_BASE+0x00000008) == 0x80000000)
      return 1;
    else if (REG32(ASCON_AEAD_DE_REG_BASE+0x00000008) == 0x40000000)
      return 1;
    else
      return 0;
}

/**
  * @brief  获取标签
  * @param  ascon_aead_initstruct
  * @retval none
  */

void Get_Ascon_Aead_De_Tag(Ascon_Aead_De_InitTypeDef* ascon_aead_initstruct)
{   
    ascon_aead_initstruct->aead_t[0] = ASCON_AEAD_DE_REG->AT0;
    ascon_aead_initstruct->aead_t[1] = ASCON_AEAD_DE_REG->AT1;
    ascon_aead_initstruct->aead_t[2] = ASCON_AEAD_DE_REG->AT2;
    ascon_aead_initstruct->aead_t[3] = ASCON_AEAD_DE_REG->AT3;
}

/**
  * @brief  获取密文
  * @param  ascon_aead_initstruct
  * @retval none
  */

void Get_Ascon_Aead_De_Pt(Ascon_Aead_De_InitTypeDef* ascon_aead_initstruct)
{   
    u32 index;
    u32 num;
    u32 aead_addr;
    u32 mode;
    static u32 aead_data[256];
    mode = ascon_aead_initstruct->aead_ctrl_mode;
    aead_addr = ascon_aead_initstruct->aead_c_addr;
    if (mode == 0x00000001)
    {
      if (ascon_aead_initstruct -> msg_block_num <= 128)
        num = ascon_aead_initstruct -> msg_block_num * 2;
      else
        num = 128 * 2;
    }
    else 
    {
      if (ascon_aead_initstruct -> msg_block_num <= 64)
        num = ascon_aead_initstruct -> msg_block_num * 4;
      else
        num = 64 * 4;
    }
    while(!Whether_Ascon_De_Aead()){}
    for (index = 0;index<num;index++)
    {
        aead_data[index] = REG32(ASCON_AEAD_DE_TX_BASE + aead_addr + index * 4);
    }
    if (REG32(ASCON_AEAD_DE_REG_BASE+0x00000008) == 0x80000000)
      Get_Ascon_Aead_De_Tag(ascon_aead_initstruct);
    ascon_aead_initstruct->aead_data = aead_data;
}


